package com.wechat.DAO.DaoImpl;

import com.wechat.DAO.UserRelationDAO;
import com.wechat.pojo.Message;
import com.wechat.pojo.UserRelation;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserRelationDAOIpml implements UserRelationDAO {
    Connection connection = null;
    PreparedStatement pst = null;
    ResultSet resultSet = null;
    @Override
    public boolean addRelation(String UID, String FID, int Relation) {
        UserRelation queryRelation = queryRelation(UID, FID);
        if(queryRelation != null)   return false;   //如果有就不加
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/wechat?useSSL=false", "root", "123456");
            PreparedStatement pst = connection.prepareStatement("INSERT INTO USER_RELATION VALUES(NULL,?,?,?)");
            pst.setString(1,UID);
            pst.setString(2,FID);
            pst.setInt(3,Relation);
            pst.execute();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            closeAll();
        }
        System.out.println(UID+"与"+FID+"成为了好友");
        return true;
    }

    @Override
    public boolean updateRelation(String UID, String FID, int Relation) {
        UserRelation queryRelation = queryRelation(UID, FID);
        if(queryRelation == null)   return false;   //如果没有就不改
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/wechat?useSSL=false", "root", "123456");
            PreparedStatement pst = connection.prepareStatement("UPDATE USER_RELATION SET RELATIONSHIP=? WHERE UID=? AND FID=?");
            pst.setInt(1,Relation);
            pst.setString(2,UID);
            pst.setString(3,FID);
            pst.execute();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            closeAll();
        }
        System.out.println(UID+"更改了"+FID+"的好友关系为："+Relation);
        return true;
    }

    @Override
    public boolean deleteRelation(String UID, String FID) {
        UserRelation queryRelation = queryRelation(UID, FID);
        if(queryRelation == null)   return false;   //如果没有就不删
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/wechat?useSSL=false", "root", "123456");
            PreparedStatement pst = connection.prepareStatement("DELETE FROM USER_RELATION WHERE UID=? AND FID=?");
            pst.setString(1,UID);
            pst.setString(2,FID);
            pst.execute();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            closeAll();
        }
        System.out.println(UID+"把好友"+FID+"删了");
        return true;
    }

    @Override
    public UserRelation queryRelation(String UID, String FID) {
        UserRelation relation = null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/wechat?useSSL=false", "root", "123456");
            PreparedStatement pst = connection.prepareStatement("SELECT * FROM USER_RELATION WHERE UID=? AND FID=?");
            pst.setString(1,UID);
            pst.setString(2,FID);
            resultSet = pst.executeQuery();
            while (resultSet.next()){
                System.out.println(resultSet.toString());
                relation = new UserRelation(resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getInt(4));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll();
            if(resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return relation;
    }

    @Override
    public List<UserRelation> getAllRelation(String UID) {
        List<UserRelation> userRelations = new ArrayList<>();
        try{
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/wechat?useSSL=false", "root", "123456");
            PreparedStatement pst = connection.prepareStatement("SELECT * FROM USER_RELATION WHERE UID=?");
            pst.setString(1,UID);
            resultSet = pst.executeQuery();
            while (resultSet.next()){
                UserRelation relation = new UserRelation(resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getInt(4));
                userRelations.add(relation);
                System.out.println(relation);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll();
            if(resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return userRelations;
    }



    private void closeAll() {
        if(pst!=null){
            try {
                pst.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
